import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
// 定义一个继承自JFrame的类，用于创建员工管理系统的用户界面
public class EmployeeManagementSystemDemo extends JFrame {
    // 用于存储员工信息的列表
    private List<Employee> employeeList;
    // 定义了一系列界面组件，包括标签、文本框和按钮
    private JLabel labelId;
    private JTextField textFieldId;
    private JLabel labelName;
    private JTextField textFieldName;
    private JLabel labelGender;
    private JTextField textFieldGender;
    private JLabel labelAge;
    private JTextField textFieldAge;
    private JLabel labelEducation;
    private JTextField textFieldEducation;
    private JLabel labelSalary;
    private JTextField textFieldSalary;
    private JLabel labelAddress;
    private JTextField textFieldAddress;
    private JLabel labelPhoneNumber;
    private JTextField textFieldPhoneNumber;

    private JButton buttonAdd;
    private JButton buttonView;
    private JButton buttonSearchById;
    private JButton buttonSearchByName;
    private JButton buttonDelete;
    private JButton buttonModify;
    private JButton buttonExit;

    // 构造函数，初始化员工管理系统
    public EmployeeManagementSystemDemo() {
        // 初始化员工列表，使用ArrayList来存储Employee对象
        employeeList = new ArrayList<>();

        // 初始化界面组件并设置其属性
        labelId = new JLabel("职工编号:");
        textFieldId = new JTextField(10);
        labelName = new JLabel("姓名:");
        textFieldName = new JTextField(10);
        labelGender = new JLabel("性别:");
        textFieldGender = new JTextField(10);
        labelAge = new JLabel("年龄:");
        textFieldAge = new JTextField(10);
        labelEducation = new JLabel("学历:");
        textFieldEducation = new JTextField(10);
        labelSalary = new JLabel("标准工资:");
        textFieldSalary = new JTextField(10);
        labelAddress = new JLabel("家庭住址:");
        textFieldAddress = new JTextField(10);
        labelPhoneNumber = new JLabel("电话号码:");
        textFieldPhoneNumber = new JTextField(10);

        buttonAdd = new JButton("录入");
        buttonView = new JButton("浏览");
        buttonSearchById = new JButton("按职工编号查询");
        buttonSearchByName = new JButton("按姓名查询");
        buttonDelete = new JButton("删除");
        buttonModify = new JButton("修改");
        buttonExit =new JButton("退出系统");

        // 为按钮设置事件监听器，当按钮被点击时会调用相应的方法
        buttonAdd.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                addEmployee();
            }
        });

        buttonView.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                viewEmployees();
            }
        });

        buttonSearchById.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                searchEmployeeById();
            }
        });

        buttonSearchByName.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                searchEmployeeByName();
            }
        });

        buttonDelete.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                deleteEmployee();
            }
        });

        buttonModify.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                modifyEmployee();
            }
        });

        buttonExit.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                exitSystem();
            }
        });

        // 设置布局管理器为GridLayout，6行2列
        setLayout(new GridLayout(6, 2));
        add(labelId);
        add(textFieldId);
        add(labelName);
        add(textFieldName);
        add(labelGender);
        add(textFieldGender);
        add(labelAge);
        add(textFieldAge);
        add(labelEducation);
        add(textFieldEducation);
        add(labelSalary);
        add(textFieldSalary);
        add(labelAddress);
        add(textFieldAddress);
        add(labelPhoneNumber);
        add(textFieldPhoneNumber);
        add(buttonAdd);
        add(buttonView);
        add(buttonSearchById);
        add(buttonSearchByName);
        add(buttonDelete);
        add(buttonModify);
        add(buttonExit);

       // 设置窗口标题、大小、默认关闭操作和可见性
        setTitle("Mrf's Group Employee Management System");
        setSize(900, 500);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
    }

    // 添加员工的方法，从文本框中获取数据，创建Employee对象并添加到列表中
    private void addEmployee() {
        int id = Integer.parseInt(textFieldId.getText());
        String name = textFieldName.getText();
        String gender = textFieldGender.getText();
        int age = Integer.parseInt(textFieldAge.getText());
        String education = textFieldEducation.getText();
        double salary = Double.parseDouble(textFieldSalary.getText());
        String address = textFieldAddress.getText();
        String phoneNumber = textFieldPhoneNumber.getText();

        Employee employee = new Employee(id, name, gender, age, education, salary, address, phoneNumber);
        employeeList.add(employee);

        JOptionPane.showMessageDialog(this, "录入成功");
        clearFields();// 清空文本框
    }

    // 查看所有员工信息的方法，将员工信息展示在对话框中
    private void viewEmployees() {
        StringBuilder sb = new StringBuilder();
        for (Employee employee : employeeList) {
            sb.append(employee.toString()).append("\n");
        }

        JOptionPane.showMessageDialog(this, sb.toString());
    }

    // 按职工编号查询员工的方法
    private void searchEmployeeById() {
        int id = Integer.parseInt(JOptionPane.showInputDialog(this, "请输入职工编号"));
        StringBuilder sb = new StringBuilder();
        for (Employee employee : employeeList) {
            if (employee.getId() == id) {
                sb.append(employee.toString()).append("\n");
            }
        }

        if (sb.length() > 0) {
            JOptionPane.showMessageDialog(this, sb.toString());
        } else {
            JOptionPane.showMessageDialog(this, "没有找到该职工");
        }
    }

    // 按姓名查询员工的方法
    private void searchEmployeeByName() {
        String name = JOptionPane.showInputDialog(this, "请输入姓名");
        StringBuilder sb = new StringBuilder();
        for (Employee employee : employeeList) {
            if (employee.getName().equals(name)) {
                sb.append(employee.toString()).append("\n");
            }
        }

        if (sb.length() > 0) {
            JOptionPane.showMessageDialog(this, sb.toString());
        } else {
            JOptionPane.showMessageDialog(this, "没有找到该职工");
        }
    }

    // 删除员工的方法，根据职工编号从列表中移除员工
    private void deleteEmployee() {
        int id = Integer.parseInt(JOptionPane.showInputDialog(this, "请输入职工编号"));

        for (int i = 0; i < employeeList.size(); i++) {
            if (employeeList.get(i).getId() == id) {
                employeeList.remove(i);
                JOptionPane.showMessageDialog(this, "删除成功");
                return;
            }
        }

        JOptionPane.showMessageDialog(this, "没有找到该职工");
    }

    // 修改员工信息的方法，根据职工编号找到员工并修改其信息
    private void modifyEmployee() {
        int id = Integer.parseInt(JOptionPane.showInputDialog(this, "请输入职工编号"));

        for (Employee employee : employeeList) {
            if (employee.getId() == id) {
                employee.setName(JOptionPane.showInputDialog(this, "请输入姓名"));
                employee.setGender(JOptionPane.showInputDialog(this, "请输入性别"));
                employee.setAge(Integer.parseInt(JOptionPane.showInputDialog(this, "请输入年龄")));
                employee.setEducation(JOptionPane.showInputDialog(this, "请输入学历"));
                employee.setSalary(Double.parseDouble(JOptionPane.showInputDialog(this, "请输入标准工资")));
                employee.setAddress(JOptionPane.showInputDialog(this, "请输入家庭住址"));
                employee.setPhoneNumber(JOptionPane.showInputDialog(this, "请输入电话号码"));

                JOptionPane.showMessageDialog(this, "修改成功");
                return;
            }
        }

        JOptionPane.showMessageDialog(this, "没有找到该职工");
    }

    // 清空所有文本框的方法
    private void clearFields() {
        textFieldId.setText("");
        textFieldName.setText("");
        textFieldGender.setText("");
        textFieldAge.setText("");
        textFieldEducation.setText("");
        textFieldSalary.setText("");
        textFieldAddress.setText("");
        textFieldPhoneNumber.setText("");
    }

    // 退出系统的方法，显示退出提示并关闭程序
    private void exitSystem() {
        JOptionPane.showMessageDialog(this, "已退出系统！");
        System.exit(0);//系统正常退出
        return ;
    }
}